package cn.kelvin.one;

public class Practice1 {
	
	public static void main(String[] args) {
//		System.out.println(oct2Bin(-128));
		System.out.println(transform("221",3,2));
		System.out.println(transform("11001",2,3));
	}
	
	static String transform(String s, int radixSrc, int radixTgt){
		String[] strs = s.split("");
		int octVal = 0,i=0,j=strs.length;
		while(i<strs.length){
			octVal = octVal + Integer.parseInt(strs[i])*((int)Math.pow(radixSrc, j-1));
			i += 1;
			j -= 1;
		}
//		System.out.println(octVal);
		StringBuilder sb = new StringBuilder();
		int temp = octVal;
		while(temp>0){
			int b = temp%radixTgt;
			temp = temp/radixTgt;
			sb.append(b);
		}
		return sb.reverse().toString();
	}
	
	static String oct2Bin(int a){
		StringBuilder sb = new StringBuilder();
		int temp = Math.abs(a);
		while(temp>0){
			int b = temp%2;
			temp = temp/2;
			sb.append(b);
		}
		String s = sb.reverse().toString();
		int len = s.length();
		if(a<0){
			s = "1"+genZeroStr(32-len-1)+s;
		}
//		else {
//			s = genZeroStr(32-len)+s;
//		}
		return s;
	}
	
	static String genZeroStr(int len){
		StringBuilder sb = new StringBuilder();
		while(len>0){
			sb.append("0");
			len = len-1;
		}
		return sb.toString();
	}
}
